package com.itheima.d11_serializable.utils;

import com.itheima.d11_serializable.entity.Account;

import java.io.*;
import java.util.ArrayList;

public class ObjectUtil {



    /**
     * 反序列化
     */
    public  static ArrayList<Account> loadAccount() throws IOException, ClassNotFoundException {
        //程序启动时，反序列化操作
        FileInputStream fis = new FileInputStream("day11_io\\src\\com\\itheima\\d11_serializable\\accounts.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);

        ArrayList<Account> accounts  = (ArrayList<Account>) ois.readObject();

        for (Account account : accounts) {
            System.out.println("加载的账户："+account);
        }

        return accounts;
    }


    /**
     * 序列化
     */
    public static void saveObject(ArrayList<Account>  accounts) throws IOException {
        FileOutputStream fos = new FileOutputStream("day11_io\\src\\com\\itheima\\d11_serializable\\accounts.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject( accounts );
        System.out.println("数据同步... ...");
    }
}
